Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fs/copy-file #2201

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from

Conversation

HRIDYANSHU054
Copy link
Contributor

@HRIDYANSHU054 HRIDYANSHU054 commented Apr 23, 2024

adds file system copy file utility to Stdlib

Description

What is the purpose of this pull request?

This pull request:

is aligned with the purpose of achievinng feature parity with Node.js fs package. It Brings file system's powerful copy file utility to Stdlib expanding its existing fs utilities. It also tends to make the copy File utility version independent for node. Moreover, with this I also wish to know with what approach do we further add abstractions over the fs utilities.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Implementation details

-- async version

function onCopy( error ) {
    if ( error ) {
        throw error;
    }
    console.log( 'src.txt has been copied to dest.txt' );
}

copyFile( 'src.txt', 'dest.txt', onCopy );

-- sync version

var copyFile = require( '@stdlib/fs/copy-file' );

// Explicitly handle the error...
var err = copyFileSync( 'src.txt', 'dest.txt' );
if ( err instanceof Error ) {
    throw err;
}

more --> currently researching on how fs/constants could be implemented for stdlibsuch that further feature parity and version independency are achievable.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@Planeshifter Planeshifter changed the title Added fs/copy file feat: add fs/copy Apr 24, 2024
@HRIDYANSHU054
Copy link
Contributor Author

Hi @Planeshifter could you rerun the CI checks so that I can rectify anything that isn't correct?

@HRIDYANSHU054
Copy link
Contributor Author

@kgryte , @Planeshifter could you rerun the CI check

@kgryte kgryte changed the title feat: add fs/copy feat: add fs/copy-file May 8, 2024
@Planeshifter Planeshifter self-requested a review May 8, 2024 19:43
@Planeshifter
Copy link
Member

/stdlib update-copyright-years

@kgryte kgryte added Feature Issue or pull request for adding a new feature. Utilities Issue or pull request concerning general utilities. labels Oct 17, 2024

mode = 0;
if ( args.length < 2 && ( !flags.source || !flags.destination ) ) {
return cli.error( new Error( 'Both source and destination files must be provided' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message should be updated based on common CLI error conventions.

if ( args.length < 2 && ( !flags.source || !flags.destination ) ) {
return cli.error( new Error( 'Both source and destination files must be provided' ) );
}
if ( flags.source ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument handling is somewhat convoluted. Either --source and --destination flags are provided OR positional arguments. Further, at L80, we resolve relative to the cwd, but not if flags are provided. We should be consistent.

}

// Perform copying...
copyFile( src, dest, mode, onCopy );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using async copy in the CLI is not necessary. Using sync is fine and can be faster in common CLI usage contexts.

@@ -0,0 +1,55 @@

{{alias}}( src, dest[, mode], clbk )
Asynchronously copies src to dest.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description should be updated to avoid abbreviations.

Modifiers for copy operation. Default: 0.

clbk: Function
Callback to invoke upon copying src file to dest file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

... if ( err ) {
... console.error( err.message );
... }
... console.log('src.txt has been copied to dest.txt');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing spaces.



{{alias}}.sync( src, dest[, mode] )
Synchronously copies src to dest.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

}

// Asynchronously copy data from src.txt to dest.txt:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

if ( !isString( dest ) && !isBuffer( dest ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be either a string or a Buffer. Value: `%s`.', dest ) );
}
if ( !isNumber( mode ) && mode !== null ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above we state that the mode must be an integer; however, here, we use a weaker check. Furthermore, are only nonnegative integers permitted?

*
* @param {(string|Buffer)} src - source filename to copy
* @param {(string|Buffer)} dest - destination filename of the copy operation
* @param {integer} [mode] - modifiers for copy operation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing default parameter value.

// MAIN //

/**
* Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it already exists.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second sentence should be moved to a note.

Comment on lines +23 to +25
var fs = require( 'fs' );
var createReadStream = require( 'fs' ).createReadStream;
var createWriteStream = require( 'fs' ).createWriteStream;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three separate imports is not necessary. Use a single import and then refer to the appropriate methods.

*
* @param {(string|Buffer)} src - source filename to copy
* @param {(string|Buffer)} dest - destination filename of the copy operation
* @param {(integer)} [mode] - modifiers for copy operation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param {(integer)} [mode] - modifiers for copy operation.
* @param {integer} [mode=0] - modifiers for copy operation

writeStream = createWriteStream( dest );
readStream.on( 'error', onReadError );
writeStream.on( 'error', onWriteError );
writeStream.on( 'finish', onFinish);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeStream.on( 'finish', onFinish);
writeStream.on( 'finish', onFinish );

* @private
*/
function onFinish() {
cb( null ); // Success
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need to provide an argument here?

@kgryte kgryte added the Needs Changes Pull request which needs changes before being merged. label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Utilities Issue or pull request concerning general utilities.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants